package requests.math.programmerCalc;

public class BinaryOperations {
	
	
	public String showOnScreen(char binary[], int ptr, String view) {
		
		/*****   Method showing on screen(Label) actual value   ******/
			
		int max = binary.length - 1;
				
		if (view.equals("bin")) {	
			String val = "";
			for (int i = 0; i <= max; i++) {
				if (binary[i] != ' ') {
					val += binary[i];			
				}
			}
			if (val.equals(""))
				val = "0";
				return val;
		}
		else if (view.equals("dec")) {
			int val = 0;
			int k = 0;
			for (int i = max; i > ptr; i--) {
				val += (int)(binary[i]-48) * (int)Math.pow(2, k);
				k++;
			}
				return Integer.toString(val);
		}
		else if (view.equals("hex")) {
			int tmp[] = new int[(max-ptr-1)/4 + 1], k = 0, length = (max-ptr-1)/4;
			for (int i = max; i > ptr; i--) {
				if (k == 4) {		
					k = 0;    length--;
				}
				if (k != 4) {
					tmp[length] += (int)(binary[i]-48) * (int)Math.pow(2, k);
					k++;
				}					
			}		
				
			String result = "";			
			for (int i = 0; i < (max-ptr-1)/4 + 1; i++) {
				switch (tmp[i]) {
					case 10: { result += 'A'; break; }
					case 11: { result += 'B'; break; }
					case 12: { result += 'C'; break; }
					case 13: { result += 'D'; break; }
					case 14: { result += 'E'; break; }
					case 15: { result += 'F'; break; }
					default: { result += Integer.toString(tmp[i]); }
				}		
			}
			if (result.equals(""))
				result = "0";
			return result;		
		}
		return null;
		
		/**********************************************************************/
		
	}
	

	public char[] Lsh(char binary[], int ptr, int max) {
		for (int i = ptr; i <= max; i++) {
			binary[i-1] = binary[i];
		}
		binary[max] = '0';
		return binary;
	}
	
	
	public char[] Rsh(char binary[], int ptr, int max) {
		for (int i = max-1; i >= ptr; i--) {
			binary[i+1] = binary[i];
		}
		binary[ptr] = ' ';
		return binary;
	}
	
	public char[] And(char binary[], char binary2[], int max) {	
		for (int i = max; i >= 0; i--) {
			if (binary[i] != ' ' && binary2[i] != ' ') {
				int tmp = (int)(binary[i]-48) + (int)(binary2[i]-48);
				if (tmp == 2) 
					binary[i] = '1';
				else 
					binary[i] = '0';
			}
			else if (binary2[i] == ' ') {
				binary[i] = ' ';
			}
			else {
				break;
			}
		}
		return binary;
	}
	
	
	public char[] Xor(char binary[], char binary2[], int max) {	
		for (int i = max; i >= 0; i--) {
			if (binary[i] != ' ' && binary2[i] != ' ') {
				int tmp = (int)(binary[i]-48) + (int)(binary2[i]-48);
				if (tmp == 1) 
					binary[i] = '1';
				else 
					binary[i] = '0';
			}
			else if (binary[i] == '1' && binary2[i] == ' ') {
				binary[i] = '1';
			}
			else if (binary[i] == ' ' && binary2[i] == '1') {
				binary[i] = '1';
			}
			else {
				break;
			}
		}
		return binary;
	}
	
	
	public char[] Not(char binary[], int max) {
		for (int i = 0; i <= max; i++) {
			if (binary[i] == '0' || binary[i] == ' ') 
				binary[i] = '1';
			else
				binary[i] = '0';
		}
		return binary;
	}

}
